package org.itboys.product.web;

import org.itboys.admin.entity.GoodsSourceDB;
import org.itboys.admin.entity.PostFeeDB;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.itboys.product.dto.GoodsSourceDTO;
import org.itboys.product.service.GoodsSourcePostFeeService;
import org.itboys.product.service.GoodsSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by liangchong on 2016/6/13.
 */
@RestController
@RequestMapping("/goods/source")
public class GoodsSourceController {

    @Autowired
    private GoodsSourceService goodsSourceService;
    @Autowired
    private GoodsSourcePostFeeService goodsSourcePostFeeService;

    @RequestMapping("/toListVM")
    public ModelAndView toListVM(HttpServletRequest request, HttpServletResponse response,Model model) {
        return new ModelAndView("/ace/product/goods-source-list");
    }

    @RequestMapping("/toPostFeeListVM")
    public ModelAndView toListVM(Long sourceGoodsId, Model model,
                                 HttpServletRequest request, HttpServletResponse response) {
        model.addAttribute("sourceGoodsId", sourceGoodsId);
        return new ModelAndView("/ace/product/post-fee-list");
    }

    @ResponseBody
    @RequestMapping("/list")
    public void list(HttpServletRequest request,HttpServletResponse response){
        Page<GoodsSourceDB> page = goodsSourceService.pageQuery(request);
        JsonPageUtils.renderJsonPage(page.getTotal(),page.getTotalPages(), page.getData(), response);
    }

    @ResponseBody
    @RequestMapping("/postFeeList")
    public void postFeeList(HttpServletRequest request,HttpServletResponse response){
        Page<PostFeeDB> page = goodsSourcePostFeeService.pageQuery(request);
        JsonPageUtils.renderJsonPage(page.getTotal(),page.getTotalPages(), page.getData(), response);
    }

    @ResponseBody
    @RequestMapping("/getSource/{id}")
    public void getOrg(@PathVariable("id")Long id,HttpServletResponse response){
        AjaxUtils.renderJson(response, goodsSourceService.getById(id));
    }

    @ResponseBody
    @RequestMapping("/save")
    public void save(@ModelAttribute GoodsSourceDB goods , HttpServletResponse response){
        try {
            if(goods.getId() == 0l){
                goodsSourceService.save(goods);
            }else {
                goodsSourceService.update(goods);
            }
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public void delete(@PathVariable("id")Long id,HttpServletResponse response){
        try {
            goodsSourceService.delete(id);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    @ResponseBody
    @RequestMapping("/getPostFee/{id}")
    public void getPostFee(@PathVariable("id")Long id,HttpServletResponse response){
        AjaxUtils.renderJson(response, goodsSourcePostFeeService.getById(id));
    }

    @ResponseBody
    @RequestMapping("/savePostFee")
    public void savePostFee(@ModelAttribute PostFeeDB feeDB , HttpServletResponse response){
        try {
            if(feeDB.getId() == 0l){
                goodsSourcePostFeeService.save(feeDB);
            }else {
                goodsSourcePostFeeService.update(feeDB);
            }
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    @ResponseBody
    @RequestMapping("/deletePostFee/{id}")
    public void deletePostFee(@PathVariable("id")Long id,HttpServletResponse response){
        try {
            goodsSourcePostFeeService.delete(id);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    /**
     * 获取所有货源地
     * @return
     */
    @RequestMapping("/findAllSource")
    public void findAllProvince(HttpServletRequest request , HttpServletResponse response){
        Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
        List<GoodsSourceDB> goodsSourceDBs = goodsSourceService.list(param);

        List<GoodsSourceDTO> dtos = goodsSourceDBs.stream().map(s -> {
            GoodsSourceDTO dto = new GoodsSourceDTO();
            dto.setSourceId(s.getId());
            dto.setSourceName(s.getName());
            return dto;
        }).collect(Collectors.toList());
        AjaxUtils.renderJson(response, dtos);
    }
}
